//
//  GWCarViewController.m
//  HappyBug
//
//  Created by liulei on 2018/12/20.
//  Copyright © 2018年 www.hc.com.cn. All rights reserved.
//

#import "GWCarViewController.h"
#import "sectionHeadView.h"
#import "ShopModel.h"
@interface GWCarViewController ()


@end

@implementation GWCarViewController{
    
    UITableView *gwctable;
    NSArray *shops;
    NSArray *goodsArrays;
    NSMutableArray *shoppingCartArray;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self initData];
    
}

-(void)initView{
    self.navigationItem.title=@"购物车"; // self.title=@"返回";
    gwctable=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
    gwctable.delegate=self;
    gwctable.dataSource=self;
    [self.view addSubview:gwctable];
}
-(void)initData{
    //读取plist文件（不在沙盒里面）
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"ShopCartList" ofType:@"plist"];
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:filePath];
    //存储店铺模型的数组
    shoppingCartArray = [[NSMutableArray alloc] init];
    //店铺数据
    shops =dict[@"data"];
    //存储店铺模型的数组
    for (NSDictionary *shop in shops)
    {
        ShopModel *shopModel = [[ShopModel alloc] init];
        shopModel.shopId =  [shop objectForKey:@"shopId"];
        shopModel.shopName = [shop objectForKey:@"shopName"];
        shopModel.isSelected = [[shop objectForKey:@"isSelected"]integerValue];
        
        NSArray *goods = shop[@"goods"] ;
        //存储商品模型的数组
        NSMutableArray *goosArray = [[NSMutableArray alloc] init];
        for (NSDictionary *goodsDict in goods)
        {
            GoodsModel *goodsModel = [[GoodsModel alloc] init];
            goodsModel.goodsId = [goodsDict objectForKey:@"goodsId"];
            goodsModel.goodsName =[goodsDict objectForKey:@"goodsName"];
            goodsModel.count = [goodsDict objectForKey:@"count"];
            goodsModel.price = [goodsDict objectForKey:@"price"];
            goodsModel.imageUrl =[goodsDict objectForKey:@"imageUrl"];
            goodsModel.isSelected = [[goodsDict objectForKey:@"isSelected"]integerValue];
            
            [goosArray addObject:goodsModel];
        }
        
        shopModel.goods = goosArray;
        [shoppingCartArray addObject:shopModel];
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    sectionHeadView *headView =[[sectionHeadView alloc]init];
    return headView;
}

//设置行高的方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{
    return 80;
}

//组下面的子条目数量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    ShopModel *shopModel=  shoppingCartArray[section];
    
    return [shopModel.goods count];
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString  *cellId=@"CellID";
    
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellId];
    if(!cell){
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    //  NSString *celltxt =[NSString stringWithFormat:@"shopId:%@,shopName:%@,isSelected:%@",shopArrays[indexPath.row][@"shopId"],shopArrays[indexPath.row][@"shopName"],shopArrays[indexPath.row][@"isSelected"]];
    //   NSString *detailcelltxt =[NSString stringWithFormat:@"goodsId:%@,goodsName:%@,count:%@",goodsArrays[indexPath.row][@"goodsId"],goodsArrays[indexPath.row][@"goodsName"],goodsArrays[indexPath.row][@"count"]];
    cell.textLabel.text=@"1";
    // cell.detailTextLabel.text=detailcelltxt;
    return cell;
    
}
//组的数量
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return  [shoppingCartArray count];
}
@end
